###################################################################################################
# Legislative Term Limits and Polarization ########################################################
# Michael Olson and Jon Rogowski ##################################################################
###################################################################################################

# This script generates results presented in Figure 1B and Table 3 in the text and Table C.3 in 
# the supplementary materials.

###################################################################################################
# working directory and packages ##################################################################
###################################################################################################

  #setwd("./termlimits_polarization_analysisdata")

###################################################################################################
# Data ############################################################################################
###################################################################################################

  dime_cont <-   read.csv("dime_contributions.csv")
  dime_cont <- dime_cont[dime_cont$state!="NE",]

###################################################################################################
# Analysis ########################################################################################
###################################################################################################

# NOTE: Outputting Tables from This File Requires Models from legprof_interactions file

# Figure 1B: Effect of Term Limits over Legislative Professionalism, Party Share Campaign Contributions

  dime_legprof_pc <- felm(pc_share~house_term_limit2*loglegprof
                          +divided_gov
                          +abs_diff
                          |state+year|0|state,data=dime_cont)
  
  pdf(paste(output_path,"prof_pc_int.pdf",sep=""),width=8,height=5)
  out <- continuous_margeff_plot(model=dime_legprof_pc,base_coef="house_term_limit2",
                          moderating_coef="loglegprof",data=dime_cont,
                          pretty_mod = "Logged Legislative Professionalism")
  print(out)
  dev.off()


# Table 3: Term Limits, Legislative Professionalism, and Party Influence  

  pc_legprof <- felm(pc_share~house_term_limit*loglegprof
                     +divided_gov
                     +abs_diff
                     |state+year|0|state,data=dime_cont)
  
  pac_legprof <- felm(pac_share~house_term_limit*loglegprof
                      +divided_gov
                      +abs_diff
                      |state+year|0|state,data=dime_cont)
  
  mech_prof_sg <- stargazer(log_legprof,pc_legprof,pac_legprof,
                            keep=c("house_term_limit","loglegprof"),
                            add.lines = list(c("Controls","\\checkmark","\\checkmark","\\checkmark"),
                                             c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                             c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                             c("Projected $R^2$",round(summary(log_legprof)$P.r.squared,3)
                                               ,paste(as.character(round(summary(pc_legprof)$P.r.squared,3)),"0",sep="")
                                               ,paste(as.character(round(summary(pac_legprof)$P.r.squared,3)),"0",sep=""))),
                            notes.append = FALSE,notes.label = "",
                            notes="\\parbox[t]{0.875\\textwidth}{\\scriptsize \\textit{Note}: Entries are linear regression coefficients with 
                            standard errors clustered on states in parentheses. Pooled polarization is based on Shor-McCarty ideal points pooled
                            across chambers. Party Share Contributions refers to the share of total state legislative campaign contributions coming
                            from political party committees; PAC Share Contributions is an analogous measure, but for Political Action Committees.
                            $^{**}$p$<$0.05, $^*$p$<$0.10 (two-tailed test).}",
                            star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                            omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                            digits=3,digits.extra=0,
                            dep.var.labels = c("{\\scriptsize Pooled Polarization}","{\\scriptsize Party Share Contributions}",
                                               "{\\scriptsize PAC Share Contributions}"),
                            covariate.labels=c("Term Limits","ln(Leg. Professionalism)",
                                               "Term Limits $\\times$ ln(Professionalism)"),table.placement = "!ht",
                            label="mechanisms_panel",
                            table.layout ="-ld-#-t-as-n",
                            title="Term Limits, Legislative Professionalism, and Party Influence")
  
  cat(mech_prof_sg, sep = '\n', file = paste(output_path,"mech_prof.tex",sep=""))
  

# Table C.3: Term Limits, Legislative Professionalism, and Party Influence  
  
  pac_unlogged_legprof <- felm(pac_share~house_term_limit*legprofscore
                               +divided_gov
                               +abs_diff
                               |state+year|0|state,data=dime_cont)
  
  pc_unlogged_legprof <- felm(pc_share~house_term_limit*legprofscore
                              +divided_gov
                              +abs_diff
                              |state+year|0|state,data=dime_cont)
  
  unlogged_mech_prof_sg <- stargazer(base_legprof,pc_unlogged_legprof,pac_unlogged_legprof,
                                     keep=c("house_term_limit","legprof"),
                                     add.lines = list(c("Controls","\\checkmark","\\checkmark","\\checkmark"),
                                                      c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                                      c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                                      c("Projected $R^2$",round(summary(base_legprof)$P.r.squared,3)
                                                        ,round(summary(pc_unlogged_legprof)$P.r.squared,3),round(summary(pac_unlogged_legprof)$P.r.squared,3))),
                                     notes.append = FALSE,notes.label = "",
                                     notes="\\parbox[t]{0.875\\textwidth}{\\scriptsize \\textit{Note}: Entries are linear regression coefficients with
                                     standard errors clustered on states in parentheses. Pooled polarization is based on Shor-McCarty ideal points pooled
                                     across chambers. Party Share Contributions refers to the share of total state legislative campaign contributions coming
                                     from political party committees; PAC Share Contributions is an analogous measure, but for Political Action Committees.
                                     $^{**}$p$<$0.05, $^*$p$<$0.10 (two-tailed test).}",
                                     star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                                     omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                                     digits=3,digits.extra=0,
                                     dep.var.labels = c("{\\scriptsize Pooled Polarization}","{\\scriptsize Party Share Contributions}",
                                                        "{\\scriptsize PAC Share Contributions}"),
                                     covariate.labels=c("Term Limits","Leg. Professionalism",
                                                        "Term Limits $\\times$ Leg. Professionalism"),table.placement = "!ht",
                                     label="unlogged_mechanisms_panel",
                                     table.layout ="-ld-#-t-as-n",
                                     title="Term Limits, Legislative Professionalism, and Party Influence: Unlogged Professionalism Measure")
  
  cat(unlogged_mech_prof_sg, sep = '\n', file = paste(output_path,"unlogged_mech_prof.tex",sep=""))
  
# reset working directory  
  
  setwd(storewd)
  